package com.yan.common.util;

import lombok.Builder;
import lombok.Data;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author yanchen
 */
@Data
@Builder
public class AjaxEntity<T> {

    /**
     * response timestamp
     */
    private String timestamp;

    /**
     * response code
     */
    private Integer status;

    /**
     * response message
     */
    private String message;

    /**
     * response data
     */
    private T data;

    private static final SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


    /**
     * 响应成功实体
     *
     * @param data 数据对象
     * @param message 返回的消息，默认为：请求成功！
     * @param status http状态码
     * @return AjaxEntity
     * @param <T> 泛型，数据对象的类型
     */
    public static <T> AjaxEntity<T> success(Integer status,String message,T data) {
        return AjaxEntity.<T>builder()
                .timestamp(SDF.format(new Date(System.currentTimeMillis())))
                .status(status)
                .message(message)
                .data(data)
                .build();
    }
    public static <T> AjaxEntity<T> success() {
        return success(HttpStatus.SUCCESS.code(),"请求成功！", null);
    }
    public static <T> AjaxEntity<T> success(String message) {
        return success(HttpStatus.SUCCESS.code(), message, null);
    }
    public static <T> AjaxEntity<T> success(String message, T data) {
        return success(HttpStatus.SUCCESS.code(), message, data);
    }



    /**
     * 响应错误实体
     * @param data 数据对象
     * @param message 返回的消息
     * @param status http状态码
     * @return AjaxEntity
     * @param <T> 泛型，数据对象的类型
     */
    public static <T> AjaxEntity<T> fail(Integer status, String message, T data) {
        return AjaxEntity.<T>builder()
                .timestamp(SDF.format(new Date(System.currentTimeMillis())))
                .status(status)
                .message(message)
                .data(data)
                .build();
    }
    public static <T> AjaxEntity<T> fail(String message) {
        return fail(HttpStatus.ERROR.code(), message, null);
    }
    public static <T> AjaxEntity<T> fail(Integer status, String message) {
        return fail(status, message,null);
    }
    public static <T> AjaxEntity<T> fail(String message, T data) {
        return fail(HttpStatus.ERROR.code(), message, data);
    }

}
